var wb_table;
var wb_controller;

function collect_selected()
{
	var ids = new Array();
	$('tr.sel td.id span', wb_table).each(function(){
		ids.push($(this).attr('id').slice(3));
	});
	return ids+'';
}

function controller_result(data)
{
	var pipe = data.indexOf('|');
	var result = data.slice(0, pipe);
	var html = $(data.slice(pipe+1));
	$('tr', html).click(function(e){
		$(this).toggleClass('sel');
	});
	$('tbody', wb_table).remove();
	$('table', wb_table).append(html);
}

$(document).ready(function() {
	wb_table = $('#wb_table');
	wb_controller = $('.controller', wb_table);
	
	$('tr', wb_table).click(function(e){
		$(this).toggleClass('sel');
	});
	$('.js_only', wb_controller).show();
	$('.del_sel', wb_controller).click(function(e){
		e.preventDefault();
		
		var answer = confirm(lang['confirm_delete']);
		if (answer){
			$.post(
				controller_url+'ajax_handler',
				{del_items : collect_selected()},
				controller_result
			);
			$('tr.sel', wb_table).removeClass('sel');
		}
		return false;
	});
	$('.trash_sel', wb_controller).click(function(e){
		e.preventDefault();
		$.post(
			controller_url+'ajax_handler',
			{trash_items : collect_selected()},
			controller_result
		);
		$('tr.sel', wb_table).removeClass('sel');
		return false;
	});
	$('.untrash_sel', wb_controller).click(function(e){
		e.preventDefault();
		
		$.post(
			controller_url+'ajax_handler',
			{untrash_items : collect_selected()},
			controller_result
		);
		$('tr.sel', wb_table).removeClass('sel');
		return false;
	});
});